﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LuxDiner.Model.Orders;
using LuxDiner.Command.Orders;
using Utils;
namespace LuxDiner.Logic.Orders
{
    public class DealOrderLogic
    {
        public static DealOrderModel NewOrder(DealOrderModel order)
        {
            if (order != null)
            {
                if (order.Items == null || order.Items.Count == 0)
                    throw new LuxDinerException("没有内容");
                if (order.CustomerID == 0)
                    throw new LuxDinerException("请选择一个客户");
                var customer = Members.CustomerLogic.GetModel(order.CustomerID);
                if (customer == null)
                    throw new LuxDinerException("无效的客户");

                order.OrderStatus = (int)OrderStatus.Waiting;
                order.CreatedOn = DateTime.Now;
                order.TotalMoney = order.Items.Sum(p => p.Quantity * p.UnitPrice);
                foreach (var it in order.Items)
                {
                    if (it.UnitPrice < 0)
                        throw new ApplicationException("无效的单价");
                    if (it.Quantity <= 0)
                        throw new ApplicationException("无效的数量");
                    var product = Products.ProductLogic.GetModel(it.ProductID);
                    if (product == null)
                        throw new ApplicationException("无效的产品");
                    it.Product = product;
                    order.CreatedOn = DateTime.Now;
                }
                return DealOrderCommand.NewOrder(order);
            }
            return null;
        }
        /// <summary>
        /// 取得最近一次交易的价格
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="SupplierID"></param>
        /// <returns></returns>
        public static decimal GetLastPrice(int productID, int customerID)
        {
            return DealOrderCommand.GetLastPrice(productID, customerID);
        }

        public static DealOrderCollection GetOrders(DealOrderQuery sea)
        {
            return DealOrderCommand.GetOrders(sea);
        }

        public static DealOrderModel GetOrder(string orderNo)
        {
            return DealOrderCommand.GetOrder(orderNo);
        }

        public static DealOrderModel GetOrderWithProductOrders(string orderNo)
        {
            var order = DealOrderCommand.GetOrderWithProductOrders(orderNo);
            order.Items.ForEach(p =>{
                decimal cou = 0;
                order.ProductOrders.ForEach(o => {
                    cou += o.Items.Where(r => r.ProductID == p.ProductID).Sum(r => r.Quantity);
                });
                p.Quantity -= cou;
            });
            order.Items.RemoveAll(p => p.Quantity <= 0);
            return order;
        }

        public static bool CancelOrder(string orderNo)
        {
            return DealOrderCommand.CancelOrder(orderNo);
        }

        public static VDealOrderDetailsCollection GetOrderDetails(DealOrderQuery sea)
        {
            return DealOrderCommand.GetOrderDetails(sea);
        }
    }
}
